<template>
  <div >
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
   <el-row>
    <el-col :xs="24" :sm="12" :lg="form.type==2?12:24">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" >
      <el-form-item label="活动昵称:" prop="title">
        <el-input v-model="form.title" class="form-width" placeholder="请输入活动昵称" />
      </el-form-item>
      <el-form-item label="封面:" prop="thumb">
          <upload-pic :thumb="form.thumb" @update:thumb="form.thumb=$event" width="100"/>
          <div style="font-size:12px;">建议尺寸：110x110</div>
        </el-form-item>
      <el-radio-group v-model="form.banner_type" style="margin-left:10px">
        <el-radio-button  :label="0">图片</el-radio-button>
        <el-radio-button :label="1">导航</el-radio-button>
      </el-radio-group>
      <template v-if="form.banner_type==0">
         
        <el-form-item label="轮播:" prop="banner">
          <upload-pic :thumb="form.banner" @update:thumb="form.banner=$event" width="100"/>
          <div style="font-size:12px;">建议尺寸：420x420</div>
        </el-form-item>
      </template>
     <template v-if="form.banner_type==1">
       <Navigation :navigation="form.navigation" @onSubmit="form.navigation=$event"/>
      </template>
      <el-form-item label="活动类型:" prop="type">
        <el-select v-model="form.type" class="form-width-10" placeholder="请选择活动类型">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="指定商品:" prop="goods_ids" v-if="form.type==1">
        <el-select v-model="form.goods_ids" class="form-width-50" clearable filterable multiple placeholder="请选择">
          <el-option
            v-for="item in goods_list"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
     <el-form-item label="路径:" prop="path" v-if="form.type==2">
        <el-input v-model="form.path" class="form-width-50" placeholder="请输入路径链接" />
      </el-form-item>
      <el-form-item label="详情:" prop="content" v-show="form.type==0">
        <rich-text ref="myQuillEditor" :content="form.content" class="form-width-50" @change="is_change=true"/>
      </el-form-item>

      <el-form-item label="排序:" prop="sort">
        <el-input-number v-model="form.sort" class="form-width-10" placeholder="请输入排序" />
        <!-- <el-input v-model="form.sort" style="width:30%" placeholder="请输入排序" type="number" /> -->
      </el-form-item>
      <el-form-item label="是否首页显示:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>

    </el-form>
        </el-col>
      <el-col v-if="form.type==2" :xs="24" :sm="12" :lg="12">
        <div class="menu_title">选择跳转地址:</div>
        <url-address @selectMenu="selectMenu"></url-address>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import richText from '@/components/rich_text/rich_text'
import urlAddress from '@/components/url_address/urlAddress'
import Navigation from '@/components/Navigation/index'

export default {
  name:"activity_form",
  components: { richText ,urlAddress,Navigation},
  data() {
    return {
      id: 0,
      is_change: undefined,
      options: [],
      goods_list: [],
      banner: '',
      form: {status:true},
      rules: {
        title: [
          { required: true, message: '请输入活动昵称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择活动类型', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.init_into()
  },
  methods: {

    selectMenu (item) {
        this.form.thumb = this.form.thumb ? this.form.thumb : item.thumb;
        this.form.title = this.form.title ? this.form.title : item.title
        this.form.path = item.path
    },
    // 提交
    onSubmit(formName) {
      const form = this.form
      if(this.form.type==0) {
        form.content = this.$refs.myQuillEditor.editor_content
      }
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/admin/Activity/save', form, 'POST').then(e => {
            this.$back()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 获取当前数据
    init_into() {
      this.get('/admin/Activity/info?id=' + this.id, 'POST').then(e => {
        this.goods_list = e.goods_list

        this.options=e.type_arr
        for(let i in e.type_arr){
          this.options[i]={ id: Number(i), title: e.type_arr[i] }
        }
        if(this.id>0){
          this.form = e.info.tpl
          this.form.status= this.form.status== 1
          this.$refs.myQuillEditor.editor_content = this.form.content

        }
        setTimeout(()=>{this.is_change=false},100)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
        this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
.menu_title {
  font-size:20px;font-weight:bold;
  padding:10px 0px;
}

</style>
